package com.guiyang.practice.mess1.controller.message;

import com.guiyang.practice.mess1.entity.MessageRecord;
import com.guiyang.practice.mess1.service.MessageRecordService;
import com.guiyang.practice.mess1.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/message/record")
public class MessageRecordController {

    @Autowired
    private MessageRecordService messageRecordService;

    @GetMapping("/list")
    public ResultVO<List<MessageRecord>> getList(@RequestParam Map<String, Object> params) {
        try {
            List<MessageRecord> records = messageRecordService.findByCondition(params);
            return ResultVO.success("获取消息记录成功", records);
        } catch (Exception e) {
            return ResultVO.error(500, "获取消息记录失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ResultVO<MessageRecord> getById(@PathVariable Long id) {
        try {
            MessageRecord record = messageRecordService.getById(id);
            if (record != null) {
                return ResultVO.success("获取消息详情成功", record);
            }
            return ResultVO.error(404, "消息记录不存在");
        } catch (Exception e) {
            return ResultVO.error(500, "获取消息详情失败: " + e.getMessage());
        }
    }

    @PostMapping
    public ResultVO<?> sendMessage(@RequestBody MessageRecord messageRecord) {
        try {
            boolean result = messageRecordService.sendMessage(messageRecord);
            if (result) {
                return ResultVO.success("消息发送成功");
            }
            return ResultVO.error(400, "消息发送失败");
        } catch (Exception e) {
            return ResultVO.error(500, "消息发送失败: " + e.getMessage());
        }
    }
}